/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.college;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 *
 * @author project
 */
public class CollegeJobWiseReport extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // HttpSession session=request.getSession();
        //int id=Integer.parseInt(request.getParameter("jobid"));
        ServletOutputStream servletOutputStream = response.getOutputStream();
        HashMap parameterMap = new HashMap();


        Connection con = Dbconn.getConn();

        InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/report/CollegeJobWiseReport.jasper");
        try {
            String jobid="",cllgid="",job_title="",cllgname="";
            Statement st=con.createStatement();
            if(request.getParameter("jobid")!=null)
                jobid=request.getParameter("jobid");
            if(request.getParameter("clgid")!=null)
                cllgid=request.getParameter("clgid");
            ResultSet rs=st.executeQuery("select jobtitle,compname from jobs,company where jobs.compid=company.compid and jobid='"+jobid+"'");
            if(rs.next()){
                job_title=rs.getString(1);
          
               cllgname =rs.getString(2);
            }
            parameterMap.put("jobid", jobid);
            parameterMap.put("cllgid", cllgid);
            parameterMap.put("jobs_jobtitle", job_title );
            parameterMap.put("company_compname", cllgname);
            JasperRunManager.runReportToPdfStream(reportStream,
                    servletOutputStream, parameterMap, con);
            response.setContentType("application/pdf");
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch (Exception e) {
// display stack trace in the browser
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
